import React, {Component} from 'react';
import './index.less';
import {base, deepClone} from 'nc-lightapp-front';
import getCtrtData from "./functions/getCtrtData";

const {NCModal, NCTable} = base;
const {Header: NCModalHeader, Body: NCModalBody, Footer: NCModalFooter} = NCModal;

class ViewCtrtModal extends Component {
    constructor(props) {
        super(props);
        this.state = {
            columns: [],
            data: []
        };
        this.getCtrtData = this.getCtrtData.bind(this);
        this.closeModal = this.closeModal.bind(this);
    }

    getCtrtData() {
        const {orgVal, table} = this.props;
        const param_str = table.getCheckedRows('info').map(item => item.data.values.pk_psnjob.value).join();
        if (!param_str) return;
        getCtrtData({
            param_str,
            pk_org: orgVal.refpk
        })
            .then((res) => {
                if (res.success) {
                    const columns = res.data.columInfo.map(item => {
                        return {
                            title: item.name, dataIndex: item.code, key: item.code, width: '120px'
                        }
                    });
                    this.setState({
                        columns,
                        data: res.data.columData
                    });
                }
            });
    }

    closeModal() {
        this.props.onClose && this.props.onClose();
        this.setState({
            columns: [],
            data: []
        });
    }

    render() {
        const {visible} = this.props;
        const {columns, data} = this.state;
        return (
            <NCModal
                show={visible}
                size="lg"
                onHide={this.closeModal}
                onEntered={this.getCtrtData}
                backdrop={'static'}
            >
                <NCModalHeader
                    closeButton={true}
                >
                    {this.props.json['hi6007-000169']/* 国际化处理： 查看合同和协议*/}
                </NCModalHeader>
                <NCModalBody>
                    {columns.length > 0 ? <NCTable
                        className='view-ctrt-table'
                        columns={columns}
                        data={data}
                        scroll={{x: true, y: true}}
                    /> : null}
                </NCModalBody>
            </NCModal>
        );
    }
}

export default ViewCtrtModal;
